package com.weight.system.order.web;

import com.weight.system.order.PO.OrderInfo;
import com.weight.system.order.VO.OrderInfoVO;
import com.weight.system.order.service.OrderInfoService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.weight.common.util.JsonResultUtil;
import com.weight.common.util.CommonUtils;
import com.weight.common.util.DateFormUtils;
import com.weight.common.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import com.weight.common.enums.ResultEnum;

/**
 * 命令记录表
 */
@RestController
@Slf4j
@RequestMapping("/orderInfo")
public class OrderInfoController {

	@Autowired
	private OrderInfoService orderInfoService;

	private String pattern = "yyyy-MM-dd";


	/**
	 * 1.查询命令记录表--列表页 
	 * @return
	 */
	@RequestMapping(value="/queryForOrderInfoList",method=RequestMethod.GET)
	public ModelAndView queryForOrderInfoList(){
			ModelAndView modelAndView = new ModelAndView();
			
			Map<Object, Object> map =new HashMap<>();
			map.put("orderStatue", 1); 

			
			//2、查询命令记录表列表
			List<OrderInfo> orderInfoList = orderInfoService.findOrderInfoByT(map);
			
			List<OrderInfoVO> orderInfoVOList = orderInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
			
			modelAndView.setViewName("order/queryForOrderInfoList");
			modelAndView.addObject("orderInfoVOList", orderInfoVOList);
			return modelAndView;
	}

	/**
	 * 2.查询命令记录表--数据刷新  
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryForOrderInfoListDG",method=RequestMethod.GET)
	public String queryForOrderInfoListDG(HttpServletRequest request){
			//1、获取查询条件
			Integer page = Integer.parseInt(request.getParameter("page"));
			Integer rows = Integer.parseInt(request.getParameter("rows"));
			
			String orderMsg = request.getParameter("orderMsg");
			String orderDesc = request.getParameter("orderDesc");
			String orderStatue = request.getParameter("orderStatue");
			String orderDate = request.getParameter("orderDate");

			Map<Object, Object> map =new HashMap<>();
			map.put("orderMsg", orderMsg);
			map.put("orderDesc", orderDesc);
			map.put("orderStatue", orderStatue);
			map.put("orderDate", DateFormUtils.getStringToDate(orderDate, pattern));

			
			//2、查询命令记录表列表
			List<OrderInfo> orderInfoList = orderInfoService.findOrderInfoByPage(page,rows,map);
			log.info("orderInfoList={}",orderInfoList);
			List<OrderInfoVO> orderInfoVOList = orderInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
			//3、返回前台
			PageResult<OrderInfoVO> result = new PageResult<OrderInfoVO>(orderInfoVOList);
			return  JSON.toJSONString(result);
	}

	/**
	 * 展示模板--转--保存模板  
	 * @param orderInfoVO
	 * @param orderInfo
	 * @return
	 */
	public void changeVOToPO(OrderInfoVO orderInfoVO,OrderInfo orderInfo){
			BeanUtils.copyProperties(orderInfoVO,orderInfo);
			orderInfo.setOrderDate(DateFormUtils.getStringToDate(orderInfoVO.getOrderDate(), pattern));
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param orderInfoVO
	 * @param orderInfo
	 * @return
	 */
	public void changePoToVO(OrderInfoVO orderInfoVO,OrderInfo orderInfo){
			BeanUtils.copyProperties(orderInfo,orderInfoVO);
			orderInfoVO.setOrderDate(DateFormUtils.getDateToString(orderInfo.getOrderDate(), pattern));
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param orderInfo
	 * @return
	 */
	public OrderInfoVO changePoToVO(OrderInfo orderInfo){
			OrderInfoVO orderInfoVO=new OrderInfoVO();
			BeanUtils.copyProperties(orderInfo,orderInfoVO);
			orderInfoVO.setOrderDate(DateFormUtils.getDateToString(orderInfo.getOrderDate(), pattern));
			

			return orderInfoVO;
	}

	/**
	 * 3.查询命令记录表--详情页
	 * @param orderInfoId
	 * @return
	 */
	@RequestMapping("/queryForOrderInfoDetail")
	public ModelAndView queryForOrderInfoDetail(@RequestParam(value="orderInfoId",required=false) String orderInfoId){
			//1.校验id
			CommonUtils.validateExistID(orderInfoId);
			
			//2.传参，查询
			Map<String, String> map=new HashMap<>();
			map.put("orderInfoId", orderInfoId);
			log.info("查询命令记录表入参，map={}",map);
			ModelAndView modelAndView = new ModelAndView();
			OrderInfo orderInfo = orderInfoService.findByOrderInfoId(map);
			
			OrderInfoVO orderInfoVO = changePoToVO(orderInfo);
			//3、返回前台
			log.info("查询命令记录表查询成功：orderInfo={}",orderInfo);
			modelAndView.setViewName("/system/order/queryForOrderInfoDetail");
			modelAndView.addObject("orderInfoVO",orderInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 3.添加、修改命令记录表操作
	 * @param orderInfoId
	 * @return
	 */
	@RequestMapping(value="/addOrderInfo")
	public ModelAndView addOrderInfo(@RequestParam(value="orderInfoId",required=false) String orderInfoId){
			//1、创建返回对象
			OrderInfoVO orderInfoVO = new OrderInfoVO();
			
			//2.判定id不为空，则查询数据库，为修改操作
			if(StringUtils.isNotBlank(orderInfoId)) {
				Map<String, String> map=new HashMap<>();
				map.put("orderInfoId", orderInfoId);
				log.info("修改操作，查询ID：orderInfoId={}",orderInfoId);
				OrderInfo orderInfo = orderInfoService.findByOrderInfoId(map);
				this.changePoToVO(orderInfoVO,orderInfo);
			}
			
			//3.数据返回给前台
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("/system/order/addOrderInfo");
			modelAndView.addObject("orderInfoVO",orderInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 5.保存 添加、修改命令记录表操作
	 * @param orderInfoVO
	 * @param bindingResult
	 * @return
	 */
	@Transactional
	@RequestMapping(value="/saveOrderInfo")
	public JsonResultUtil saveOrderInfo(@Valid OrderInfoVO orderInfoVO,BindingResult bindingResult){
			String operateStr="add";
			//1.校验入参规则
			CommonUtils.validateBindingResult(bindingResult,orderInfoVO);
			
			//2.获取id
			String orderInfoId=orderInfoVO.getOrderInfoId();
			OrderInfo orderInfo=new OrderInfo();
			BeanUtils.copyProperties(orderInfoVO,orderInfo);
			this.changeVOToPO(orderInfoVO,orderInfo);
			
			//3.判定id不为空，则为修改操作
			if(StringUtils.isNotBlank(orderInfoId)) {
				operateStr="edit";
				Map<String, String> map=new HashMap<>();
				map.put("orderInfoId", orderInfoId);
				log.info("修改操作，查询ID：orderInfoId={}",orderInfoId);
				//查询是否存在
				orderInfoService.findByOrderInfoId(map);
				try{
					orderInfo.setUpdateBy("000");
					orderInfoService.updateOrderInfo(orderInfo);
					log.info("更新操作成功：orderInfo={}",orderInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}else {//判定id为空，则为添加操作
				try{
					orderInfo.setOrderInfoId(CommonUtils.getUUId());
					orderInfo.setCreateBy("000");
					orderInfo.setUpdateBy("000");
					orderInfoService.insertOrderInfo(orderInfo);
					log.info("添加操作成功：orderInfo={}",orderInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}
			return new JsonResultUtil(ResultEnum.SUCCESS, "操作成功！", operateStr);
	}

	/**
	 * 6.删除命令记录表操作
	 * @param orderInfoId
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByOrderInfoId")
	public JsonResultUtil deleteByOrderInfoId(@RequestParam(value="orderInfoId",required=false) String orderInfoId){
			//1.校验id
			CommonUtils.validateExistID(orderInfoId);
			
			//2.校验数据库中是否存在!
			Map<String, String> map=new HashMap<>();
			map.put("orderInfoId", orderInfoId);
			orderInfoService.findByOrderInfoId(map);
			//3.传参,添加操作人 删除
			map.put("updateBy","000");
			log.info("删除命令记录表入参，map={}",map);
			
			try{
				orderInfoService.upDelByOrderInfoId(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

	/**
	 * 6.删除命令记录表操作---批量删除
	 * @param orderInfoIdList
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByOrderInfoIdBatch")
	public JsonResultUtil deleteByOrderInfoIdBatch(@RequestParam(value="orderInfoIdList",required=false) List orderInfoIdList){
			//1.校验id
			CommonUtils.validateExistIDList(orderInfoIdList);
			
			//2.校验数据库
			orderInfoService.findByOrderInfoIdList(orderInfoIdList);
			//3.传参 
			Map<String, Object> map=new HashMap<>();
			map.put("orderInfoIdList", orderInfoIdList);
			map.put("updateBy","000");
			log.info("批量删除命令记录表操作入参，map={}",map);
			
			try{
				orderInfoService.upDelByOrderInfoIdBatch(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "批量删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

}